package com.yunfeng.supercall.activity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;

import com.ly.quickdev.library.utils.JsonUtils;
import com.yunfeng.supercall.R;
import com.yunfeng.supercall.bean.ContactsBaseBean;
import com.yunfeng.supercall.bean.SuperFriendsBean;
import com.yunfeng.supercall.httputils.MyAjaxCallBack;

public class FriendsListByGroupActivity extends
		BaseStickyHeaderListActivity<SuperFriendsBean> {

	private int mGroupId;

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		SuperFriendsBean mFriends = mList.get(position);
		// if (mGroupId != -1) {
		startActivity(new Intent(this, ChatActivity.class).putExtra(KEY_DATA,
				mFriends).putExtra(KEY_CAN_NOT_DIAL, mGroupId == -2));
		// } else {
		// startActivity(new Intent(this, FriendsDetailActivity.class)
		// .putExtra(KEY_DATA, mFriends));
		// }
	}

	@Override
	public void loadData() {
		mGroupId = getIntent().getIntExtra(KEY_DATA, 0);
		if (mGroupId == -1) {
			load(FUNCTION_FENSI_LIST);
			setTitleBar(R.string.fans_list);
		} else if (mGroupId == -2) {
			load(FUNCTION_GUANZHU_LIST);
			setTitleBar(R.string.focused_list);
		} else {
			setTitleBar(R.string.group_friend);
			JSONObject json = getBaseParams(getActivity());
			try {
				json.put("i_groupid", mGroupId);
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			mHttpClient.post(FUNCTION_GET_GROUP_USER, json,
					new MyAjaxCallBack() {

						@Override
						public void onReceiveError(String msg, String errorCode) {
							// TODO Auto-generated method stub

						}

						@Override
						public void onReceiveData(String data, String msg) {
							// TODO Auto-generated method stub
							mList = JsonUtils.parseList(data,
									SuperFriendsBean.class);
							setListAdapter();
						}
					});
		}
	}

	private void load(String url) {
		JSONObject json = getBaseParams(getActivity());
		try {
			json.put("i_groupid", mGroupId);
			json.put("i_pagenum", 0);
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		mHttpClient.post(url, json, new MyAjaxCallBack() {

			@Override
			public void onReceiveError(String msg, String errorCode) {
				// TODO Auto-generated method stub

			}

			@Override
			public void onReceiveData(String data, String msg) {
				// TODO Auto-generated method stub
				mList = JsonUtils.parseList(data, SuperFriendsBean.class);
				setListAdapter();
			}
		});
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = getLayoutInflater().inflate(
					R.layout.list_item_super_friends, null);
		}
		ContactsBaseBean base = mList.get(position);
		ContactsBaseBean.setupSingleLineContactsView(convertView, base);

		return convertView;
	}

}
